var ss = (function () {

    var ss = {
        dynamicFormPost: function (url, data, token) {
            // token预留
            var f = document.createElement('form');
            f.setAttribute("method", 'post');
            f.setAttribute('action', url);

            if (data !== undefined) {
                for (key in data) {
                    var value = data[key];
                    var i = document.createElement('input');
                    i.setAttribute('name', key);
                    i.setAttribute('value', value);
                    f.appendChild(i);
                }
            }

            if (token !== undefined) {
                var i = document.createElement('input');
                i.setAttribute('name', 'csrf_token');
                i.setAttribute('value', token);
                i.appendChild(i);
            }

            document.getElementsByTagName('body')[0].appendChild(f);
            f.submit();
        },
        order: function () {
            var $this = $(this);
            var pid = $this.attr('data-value');
            var url = $this.attr('data-url');
            ss.dynamicFormPost(url, {product: pid});
        }
    };

    return ss;
})();


$(function () {

    $(".btn-month").on('click', ss.order);
    $(".btn-year").on('click', ss.order);

    function stripTrailingSlash(str) {
        if (str.substr(-1) == '/') {
            return str.substr(0, str.length - 1);
        }
        return str;
    }

    var url = window.location.pathname;
    var activePage = stripTrailingSlash(url);

    $('.btn-nav ul li a').each(function () {
        var currentPage = stripTrailingSlash($(this).attr('href'));

        if (activePage == currentPage) {
            $(this).parent().addClass('active');
        }
    });
});